package com.leyou.item.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leyou.common.enums.ExceptionEnum;
import com.leyou.common.exception.LyException;
import com.leyou.common.utils.BeanHelper;
import com.leyou.item.dto.CategoryDTO;
import com.leyou.item.entity.Category;
import com.leyou.item.mapper.CategoryMapper;
import com.leyou.item.service.CategoryService;

import org.junit.Test;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements
        CategoryService {
    @Override
    public List<CategoryDTO> findCategoryListByParentId(Long pid) {
        //设置查询条件
        QueryWrapper<Category> queryWrapper = new QueryWrapper<>();
        //设置查询条件
        //queryWrapper.eq("parent_id",pid);
       // SFunction<Category, Long> getParentId = Category->Category.getParentId();等价但本次不能换
        //函数Category-->Long
        //Category::getParentId;
      //  Long apply = getParentId.apply(new Category());
        //https://bbs.csdn.net/topics/392651359
        queryWrapper.lambda().eq(Category::getParentId,pid);
        List<Category> categoryList = this.list(queryWrapper);
        if(CollectionUtils.isEmpty(categoryList)){
            throw  new LyException(ExceptionEnum.CATEGORY_NOT_FOUND);
        }
        return BeanHelper.copyWithCollection(categoryList,CategoryDTO.class);
    }

    @Override
    public  List<CategoryDTO> findCategoryListByBandId(Long id) {
        List<Category> categoryList = this.getBaseMapper().findCategoryListByBandId(id);
        if(CollectionUtils.isEmpty(categoryList)){
            throw  new LyException(ExceptionEnum.CATEGORY_NOT_FOUND);
        }
        return BeanHelper.copyWithCollection(categoryList,CategoryDTO.class);
    }

    @Override
    public List<CategoryDTO> findCategoryByCid3(Long id) {
        List<Category> categories= this.baseMapper.findCategoryByCid3(id);
        List<CategoryDTO> categoryDTOS = BeanHelper.copyWithCollection(categories, CategoryDTO.class);
        return categoryDTOS;
    }
    @Test
    public void djj(){
        String str ="sdfhfhdhfjksdjfjsd李数据计算 123";
        char[] chars = str.toCharArray();
        int number =0;
        int blank=0;
        int a=0;
        int cha=0;
        for (char aChar : chars) {
            if (aChar==' '){
                blank++;
            }else if (aChar>='A'&&aChar<='z'){
                a++;
            }else if (aChar>='0'&&aChar<='9'){
                number++;
            }else {
                cha++;
            }
        }
        System.out.println(number+"   "+blank+"   "+a+"   "+cha);
    }
}
